home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / amiga / ixmlb920.lha / stdio_2 / rcs / findfp.c,v < prev    next >
Text File  |  1992-07-04  |  6KB  |  248 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    92.06.08.15.10.10;    author mwild;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @initial checkin
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*-
  26.  * Copyright (c) 1990 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * This code is derived from software contributed to Berkeley by
  30.  * Chris Torek.
  31.  *
  32.  * Redistribution and use in source and binary forms, with or without
  33.  * modification, are permitted provided that the following conditions
  34.  * are met:
  35.  * 1. Redistributions of source code must retain the above copyright
  36.  *    notice, this list of conditions and the following disclaimer.
  37.  * 2. Redistributions in binary form must reproduce the above copyright
  38.  *    notice, this list of conditions and the following disclaimer in the
  39.  *    documentation and/or other materials provided with the distribution.
  40.  * 3. All advertising materials mentioning features or use of this software
  41.  *    must display the following acknowledgement:
  42.  *    This product includes software developed by the University of
  43.  *    California, Berkeley and its contributors.
  44.  * 4. Neither the name of the University nor the names of its contributors
  45.  *    may be used to endorse or promote products derived from this software
  46.  *    without specific prior written permission.
  47.  *
  48.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  49.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  52.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  54.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  58.  * SUCH DAMAGE.
  59.  */
  60.  
  61. #if defined(LIBC_SCCS) && !defined(lint)
  62. static char sccsid[] = "@@(#)findfp.c    5.10 (Berkeley) 2/24/91";
  63. #endif /* LIBC_SCCS and not lint */
  64.  
  65. #define KERNEL
  66. #include "ixemul.h"
  67.  
  68. #include <unistd.h>
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include "local.h"
  73. #include "glue.h"
  74.  
  75. #if 0
  76. int    __sdidinit;
  77. #endif
  78.  
  79. #if 0
  80. #define NSTATIC    20    /* stdin + stdout + stderr + the usual */
  81. #else
  82. #define NSTATIC 0
  83. #endif
  84. #define    NDYNAMIC 10    /* add ten more whenever necessary */
  85.  
  86. #if 0
  87. #define    std(flags, file) \
  88.     {0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite}
  89. /*     p r w flags file _bf z  cookie      close    read    seek    write */
  90.  
  91. static FILE usual[NSTATIC - 3];    /* the usual */
  92. static struct glue uglue = { 0, NSTATIC - 3, usual };
  93.  
  94. FILE __sF[3] = {
  95.     std(__SRD, STDIN_FILENO),        /* stdin */
  96.     std(__SWR, STDOUT_FILENO),        /* stdout */
  97.     std(__SWR|__SNBF, STDERR_FILENO)    /* stderr */
  98. };
  99. struct glue __sglue = { &uglue, 3, __sF };
  100. #else
  101. void
  102. __init_stdinouterr (void)
  103. {
  104.   FILE *fp;
  105.  
  106.   fp = u.u_sF[0] = __sfp();
  107.   if (fp)
  108.     {
  109.       fp->_flags  = __SRD;
  110.       fp->_file   = STDIN_FILENO;
  111.       fp->_cookie = fp;
  112.       fp->_close  = __sclose;
  113.       fp->_read   = __sread;
  114.       fp->_seek   = __sseek;
  115.       fp->_write  = __swrite;
  116.     }
  117.  
  118.   fp = u.u_sF[1] = __sfp();
  119.   if (fp)
  120.     {
  121.       fp->_flags  = __SWR;
  122.       fp->_file   = STDOUT_FILENO;
  123.       fp->_cookie = fp;
  124.       fp->_close  = __sclose;
  125.       fp->_read   = __sread;
  126.       fp->_seek   = __sseek;
  127.       fp->_write  = __swrite;
  128.     }
  129.  
  130.   fp = u.u_sF[2] = __sfp();
  131.   if (fp)
  132.     {
  133.       fp->_flags  = __SRW|__SNBF;
  134.       fp->_file   = STDERR_FILENO;
  135.       fp->_cookie = fp;
  136.       fp->_close  = __sclose;
  137.       fp->_read   = __sread;
  138.       fp->_seek   = __sseek;
  139.       fp->_write  = __swrite;
  140.     }
  141. }
  142. #endif
  143.  
  144. static struct glue *
  145. moreglue(n)
  146.     register int n;
  147. {
  148.     register struct glue *g;
  149.     register FILE *p;
  150.     const static FILE empty;    /* this static is ok in the library */
  151.  
  152.     g = (struct glue *)malloc(sizeof(*g) + n * sizeof(FILE));
  153.     if (g == NULL)
  154.         return (NULL);
  155.     p = (FILE *)(g + 1);
  156.     g->next = NULL;
  157.     g->niobs = n;
  158.     g->iobs = p;
  159.     while (--n >= 0)
  160.         *p++ = empty;
  161.     return (g);
  162. }
  163.  
  164. /*
  165.  * Find a free FILE for fopen et al.
  166.  */
  167. FILE *
  168. __sfp()
  169. {
  170.     register FILE *fp;
  171.     register int n;
  172.     register struct glue *g;
  173.  
  174. #if 0
  175.     if (!__sdidinit)
  176.         __sinit();
  177. #endif
  178.     for (g = &__sglue;; g = g->next) {
  179.         for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
  180.             if (fp->_flags == 0)
  181.                 goto found;
  182.         if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
  183.             break;
  184.     }
  185.     return (NULL);
  186. found:
  187.     fp->_flags = 1;        /* reserve this slot; caller sets real flags */
  188.     fp->_p = NULL;        /* no current pointer */
  189.     fp->_w = 0;        /* nothing to read or write */
  190.     fp->_r = 0;
  191.     fp->_bf._base = NULL;    /* no buffer */
  192.     fp->_bf._size = 0;
  193.     fp->_lbfsize = 0;    /* not line buffered */
  194.     fp->_file = -1;        /* no file */
  195. /*    fp->_cookie = <any>; */    /* caller sets cookie, _read/_write etc */
  196.     fp->_ub._base = NULL;    /* no ungetc buffer */
  197.     fp->_ub._size = 0;
  198.     fp->_lb._base = NULL;    /* no line buffer */
  199.     fp->_lb._size = 0;
  200.     return (fp);
  201. }
  202.  
  203. /*
  204.  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
  205.  * but documented historically for certain applications.  Bad applications.
  206.  */
  207. f_prealloc()
  208. {
  209.     int n = getdtablesize() - NSTATIC + 20;        /* 20 for slop */
  210.     register struct glue *g;
  211.  
  212.     for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
  213.         /* void */;
  214.     if (n > 0)
  215.         g->next = moreglue(n);
  216. }
  217.  
  218.  
  219. /* this is put into a plain atexit() chain for stdio inside the library */
  220.  
  221. /*
  222.  * exit() calls _cleanup() through *__cleanup, set whenever we
  223.  * open or buffer a file.  This chicanery is done so that programs
  224.  * that do not use stdio need not link it all in.
  225.  *
  226.  * The name `_cleanup' is, alas, fairly well known outside stdio.
  227.  */
  228. void
  229. _cleanup()
  230. {
  231.     /* (void) _fwalk(fclose); */
  232.     (void) _fwalk(__sflush);        /* `cheating' */
  233. }
  234.  
  235. #if 0
  236. /*
  237.  * __sinit() is called whenever stdio's internal variables must be set up.
  238.  */
  239. void
  240. __sinit()
  241. {
  242.     /* make sure we clean up on exit */
  243.     __cleanup = _cleanup;        /* conservative */
  244.     __sdidinit = 1;
  245. }
  246. #endif
  247. @
  248.